package com.ldl.origami.util;


import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.HashMap;
import java.util.Properties;

/**
 * Servlet implementation class InitServlet
 */
public class InitServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

    //用户默认头像
    public static String defaultHeadImg;

    /**
     * 积分获取
     */
    //注册获取积分
    public static int Integral_register;
    //每日第一次登录获取积分
    public static int Integral_first_login;
    //数据库地址
    public static String dataURL;
    //数据库用户名
    public static String dataName;
    //数据库密码
    public static String dataPasswd;
    //等级称号对应积分
    public static HashMap<String,String> levelMap;
    //cookie有效时间
    public static int cookieTime;
    //教程评分获得积分
    public static int Integral_score;
    //首页每页显示数
    public static int indexLimit;
    //首页排序规则
    public static String indexOrder;
    //热门排行查询总数
    public static int hotlistTotalCount;
    //热门排行每页显示数
    public static int hotlistLimit;
    //热门排行总页数
    public static int hotlistPage;
    //热门排行排序规则
    public static String hotlistOrder;
    //最新发布查询总数
    public static int newlistLimit;
    //最新发布每页显示数
    public static int newlistTotalCount;
    //最新发布排序规则
    public static String newlistOrder;
    //最新发布总页数
    public static int newlistPage;
    //我的收藏每页记录数
    public static int myCollectionLimit;
    //我的消息每页记录数
    public static int myNewsLimit;
    //我的教程每页记录数
    public static int myTutorialsLimit;
    @Override
	public void init(ServletConfig config) throws ServletException {
		// TODO Auto-generated method stub
		super.init();

        //读取配置文件
        String path = config.getInitParameter("configPath");
        Properties p = new Properties();
        try {
            InputStream is = new FileInputStream(path);
            p.load(is);
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }


        defaultHeadImg = p.getProperty("defaultHeadImg");
        Integral_register = Integer.parseInt(p.getProperty("Integral_register"));
        Integral_first_login = Integer.parseInt(p.getProperty("Integral_first_login"));
        Integral_score = Integer.parseInt(p.getProperty("Integral_score"));
        dataURL = p.getProperty("dataURL");
        dataName = p.getProperty("dataName");
        dataPasswd = p.getProperty("dataPasswd");
        cookieTime = Integer.parseInt(p.getProperty("cookieTime"));
        indexLimit = Integer.parseInt(p.getProperty("indexLimit"));
        indexOrder = p.getProperty("indexOrder");
        hotlistLimit = Integer.parseInt(p.getProperty("hotlistLimit"));
        hotlistOrder = p.getProperty("hotlistOrder");
        hotlistTotalCount = Integer.parseInt(p.getProperty("hotlistTotalCount"));
        newlistLimit = Integer.parseInt(p.getProperty("newlistLimit"));
        newlistOrder = p.getProperty("newlistOrder");
        newlistTotalCount = Integer.parseInt(p.getProperty("newlistTotalCount"));
        hotlistPage = Integer.parseInt(p.getProperty("hotlistPage"));
        newlistPage = Integer.parseInt(p.getProperty("newlistPage"));
        myCollectionLimit = Integer.parseInt(p.getProperty("myCollectionLimit"));
        myNewsLimit = Integer.parseInt(p.getProperty("myNewsLimit"));
        myTutorialsLimit = Integer.parseInt(p.getProperty("myTutorialsLimit"));
        //读取数据库
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        String sql_level = "SELECT level_name,level_integral FROM LEVEL";
        try {
            Class.forName("com.mysql.jdbc.Driver");
            connection = DriverManager.getConnection(dataURL, dataName, dataPasswd);
            //取得积分和等级称号关系
            preparedStatement = connection.prepareStatement(sql_level);
            resultSet = preparedStatement.executeQuery();
            levelMap = new HashMap<>();
            while(resultSet.next()){
                levelMap.put(resultSet.getString("level_name"),resultSet.getString("level_integral"));
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {

            try {
                if (connection != null) {
                    connection.close();
                }
                if (preparedStatement != null) {
                    preparedStatement.close();
                }
                if (resultSet != null) {
                    resultSet.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }

        }

    }
	

}
